package com.mingdinggroup.app.di.module;

import android.app.Application;

import com.mingdinggroup.app.application.App;
import com.mingdinggroup.app.util.NetWorkUtil;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by jessyan on 2016/3/14.
 */
@Module
public class HttpClientModule {
    /**
     * 单位秒
     */
    private static final int TIME_OUT = 10;
    private static final int CACHE_SIZE = 100;
    private final String CACHE_NAME = "cache";




    @Singleton
    @Provides
    Retrofit provideRetrofit(Application application, Retrofit.Builder builder, OkHttpClient client, HttpUrl httpUrl) {
        builder
                .baseUrl(httpUrl)//域名
                .client(client)//设置okhttp
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//使用rxjava
                .addConverterFactory(GsonConverterFactory.create());//使用Gson
        return builder.build();
    }


    @Singleton
    @Provides
    OkHttpClient provideClient(Interceptor HeadInterceptor, Interceptor CacheInterceptor) {
        File cacheFile = new File(App.mApp.getCacheDir(), CACHE_NAME);
        Cache cache = new Cache(cacheFile, CACHE_SIZE*1024 * 1024); //100Mb
        OkHttpClient okHttpClient = new OkHttpClient.Builder().readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .addInterceptor(HeadInterceptor)
                .addNetworkInterceptor(CacheInterceptor).cache(cache).build();
        return okHttpClient;
    }


    @Singleton
    @Provides
    Retrofit.Builder provideRetrofitBuilder() {
        return new Retrofit.Builder();
    }


    @Singleton
    @Provides
    Interceptor provideInterceptor() {
        return new CacheInterceptor();//打印请求信息的拦截器
    }

    @Singleton

    @Provides
    Interceptor provideHeadInterceptor() {
        return new HeadInterceptor();
    }


    class CacheInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (!NetWorkUtil.isNetConnected(App.mApp)) {
                request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
            }

            Response originalResponse = chain.proceed(request);
            if (NetWorkUtil.isNetConnected(App.mApp)) {
                //有网的时候读接口上的@Headers里的配置，你可以在这里进行统一的设置
                String cacheControl = request.cacheControl().toString();
                return originalResponse.newBuilder().header("Cache-Control", cacheControl).removeHeader("Pragma").build();
            } else {
                return originalResponse.newBuilder()
                        .header("Cache-Control", "public, only-if-cached, max-stale=2419200")
                        .removeHeader("Pragma").build();
            }
        }
    }


    class HeadInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request().newBuilder()
//                    .addHeader("Content-Type", "application/json")
                    .build());
        }
    }


}
